require "function"
require "G_ui"
local bb = require("badboy")
bb.loadutilslib()
bb.loaduilib()
local pos = bb.loadpos()
local json = bb.getJSON()
init("0", 1); --以当前应用 Home 键在右边初始化
local width, height = getScreenSize()
setUIOrientation(1)
print(width .. " " .. height)
toast("当前设备分辨率为!" .. width .. "*" .. height);

uis = {}
points = {}
swips = {}
otherpoints = {}
lastMepoint = { 22, 13 }

if width == 750 and height == 1334 then
	require "d_750"
elseif width == 720 and height == 1280 then
	require "d_720"
elseif width == 1080 and height == 1920 then
	toast("人机对战暂时只支持720*1280!");
	require "d_1080"
else
	toast("暂不支持此分辨率!");
	lua_exit()
end
ggg, err = getCloudContent("wangzherongyao", "2757EECDA00C9D5E", "没有正确获取到公告信息")

content = getUIContent("ui.json")
lua_value = json.decode(content)   --对获取到的json字符串解码
lua_value.views[4].views[1].text = ggg

--重新编码json字符串，窗口将按照新设定的尺寸显示
local ret, resgg = showUI(json.encode(lua_value))
print(resgg)

function 乱七八糟()
	if findCurrentUI(uis.挑战界面) then
		print("挑战界面")
		local zhangjie = resgg.章节
		local xuanzhong = false; -- 选中章节
		if zhangjie == "0" then
			--陨落的废都
			local zhangjiepoint = findCurrentUI(uis.陨落的废都)
			--如果找到了陨落的废都,就直接点击
			if zhangjiepoint then
				tap(zhangjiepoint[1], zhangjiepoint[2])
				xuanzhong = true;
			else
				local swippoint = swips.挑战界面.选择场景
				swip(swippoint[1], swippoint[2], swippoint[3], swippoint[4])
			end
		elseif zhangjie == "1" then
			--稷下的学院
			local zhangjiepoint = findCurrentUI(uis.稷下的学院)
			--如果找到了陨落的废都,就直接点击
			if zhangjiepoint then
				tap(zhangjiepoint[1], zhangjiepoint[2])
				xuanzhong = true;
				
			end
		elseif zhangjie == "2" then
			--灭国的战场
			local zhangjiepoint = findCurrentUI(uis.灭国的战场)
			--如果找到了陨落的废都,就直接点击
			if zhangjiepoint then
				tap(zhangjiepoint[1], zhangjiepoint[2])
				xuanzhong = true;
				
			end
		elseif zhangjie == "3" then
			--堕落的祸源
			local zhangjiepoint = findCurrentUI(uis.堕落的祸源)
			--如果找到了陨落的废都,就直接点击
			if zhangjiepoint then
				tap(zhangjiepoint[1], zhangjiepoint[2])
				xuanzhong = true;
				
			else
				local swippoint = swips.挑战界面.选择场景
				swip(swippoint[3], swippoint[4], swippoint[1], swippoint[2])
			end
		end
		
		--选中了章节才去选难度和关卡
		if xuanzhong then
			mSleep(500)
			local nandu = resgg.难度
			local nandupoint = {}
			if nandu == "0" then
				nandupoint = points.挑战界面.普通
			elseif nandu == "1" then
				nandupoint = points.挑战界面.精英
			elseif nandu == "2" then
				nandupoint = points.挑战界面.大师
			end
			tap(nandupoint[1], nandupoint[2])
			
			mSleep(500)
			local guanqia = resgg.关卡
			local guanqiapoing = {}
			if guanqia == "0" then
				guanqiapoing = points.挑战界面.第一关
			elseif guanqia == "1" then
				guanqiapoing = points.挑战界面.第二关
			elseif guanqia == "2" then
				guanqiapoing = points.挑战界面.第三关
			elseif guanqia == "3" then
				guanqiapoing = points.挑战界面.第四关
			end
			
			tap(guanqiapoing[1], guanqiapoing[2])
			mSleep(500)
			
			local next = points.挑战界面.下一步
			tap(next[1], next[2])
		end
		
		
	end
	
	if findCurrentUI(uis.挑战准备界面) then
		local start = points.挑战准备界面.闯关
		tap(start[1], start[2])
	end
	
	if findCurrentUI(uis.挑战结算界面) then
		--判断金币是否上限
		local stopbyCoin = resgg.休息
		if stopbyCoin == "1" then
			local repeatFire = points.挑战结算界面.再次挑战
			if repeatFire ~= nil then
				tap(repeatFire[1], repeatFire[2])
			end
		else
			
			if findCurrentUI(uis.今日金币上限) then
				--如果金币满了就退出脚本
				lua_exit()
			else
				local repeatFire = points.挑战结算界面.再次挑战
				if repeatFire ~= nil then
					tap(repeatFire[1], repeatFire[2])
				end
			end
		end
	end
end

function ControlChoose()
	local neirong = resgg.内容
	--冒险模式
	if neirong == "0" then
		if findCurrentUI(uis.主界面) then
			print("主界面")
			local point = points.主界面.冒险模式
			if point ~= nil then
				tap(point[1], point[2])
			end
		end
		
		if findCurrentUI(uis.冒险界面) then
			print("冒险界面")
			local point = points.冒险界面.挑战模式
			if point ~= nil then
				tap(point[1], point[2])
			end
		end
		
		if findCurrentUI(uis.人机英雄选择界面) then
			print("人机英雄选择界面")
		else 
			乱七八糟()
		end
		
		--[[
		["章节"] = "1",
		["难度"] = "2",
		["休息"] = "0",
		["关卡"] = "2",
		]]--
		
	elseif neirong == "1" then
		if findCurrentUI(uis.主界面) then
			print("主界面")
			local point = points.主界面.对战模式
			if point ~= nil then
				tap(point[1], point[2])
			end
		end
		
		if findCurrentUI(uis.对战模式) then
			print("对战模式")
			local point = points.对战模式.人机练习
			if point ~= nil then
				tap(point[1], point[2])
			end
		end
		if findCurrentUI(uis.人机英雄选择界面) then
			print("人机英雄选择界面")
			quickTap(points.人机对战英雄选择.展开)
			mSleep(1000)
			local heroType = resgg.英雄类型
			local column = resgg.列数
			local row = resgg.行数
			if heroType == "0" then
				quickTap(points.人机对战英雄选择.全部)
				
			elseif heroType == "1" then
				quickTap(points.人机对战英雄选择.坦克)
				
			elseif heroType == "2" then
				quickTap(points.人机对战英雄选择.战士)
				
			elseif heroType == "3" then
				quickTap(points.人机对战英雄选择.刺客)
				
			elseif heroType == "4" then
				quickTap(points.人机对战英雄选择.法师)
				
			elseif heroType == "5" then
				quickTap(points.人机对战英雄选择.射手)
				
			elseif heroType == "6" then
				quickTap(points.人机对战英雄选择.辅助)
			end
			mSleep(1000)
			local point = points.人机对战英雄选择.第一行
			
			x = point[1] + (column * 130)
			y = point[2] + (row *130)
			tap(x,y)
			mSleep(1000)
			
		end
		
		if findCurrentUI(uis.人机对战) then
			print("人机对战")
			
			local 人机模式 = resgg.人机模式
			local 人机难度 = resgg.人机难度
			sysLog("人机难度" .. 人机难度)
			local point = {}
			if 人机难度 == "0" then
				point = findCurrentUI(uis.入门)
			elseif 人机难度 == "1" then
				point = findCurrentUI(uis.简单)
			elseif 人机难度 == "2" then
				point = findCurrentUI(uis.一般)
			end
			
			if point ~= nil then
				tap(point[1], point[2])
			else
				if 人机模式 == "0" then
					local point = points.人机对战.王者峡谷
					if point ~= nil then
						tap(point[1], point[2])
					end
				elseif 人机模式 == "1" then
					local point = points.人机对战.深渊大乱斗
					if point ~= nil then
						tap(point[1], point[2])
					end
				elseif 人机模式 == "2" then
					local point = points.人机对战.长平攻防战
					if point ~= nil then
						tap(point[1], point[2])
					end
				elseif 人机模式 == "3" then
					local point = points.人机对战.墨家机关道
					if point ~= nil then
						tap(point[1], point[2])
					end
				end
			end
		end
		
	end
end

function fireinGame()
	if findCurrentUI(uis.王者峡谷游戏中) then
		print("王者峡谷游戏中")
		FindGamePoint()
		local p1 = nil
		--先获取自己小地图的坐标
		local mePoint = findCurrentUI(uis.小地图自己坐标)
		if mePoint == nil then
			mePoint = lastMepoint
		else
			lastMepoint = mePoint;
		end
		
		p1 = pos:new(mePoint[1], mePoint[2])
		print("找到小地图自己坐标" .. mePoint[1] .. " " .. mePoint[2])
		
		local point = points.游戏中.方向键
		local 英雄被攻击 = findCurrentUI(uis.英雄被攻击)
		if 英雄被攻击 ~= nil then
			touchDown(1, point[1], point[2]);
			mSleep(200);
			touchMove(1, point[1] - 110, point[2] + 180)
			mSleep(1500);
			touchUp(1,point[1],point[2])
		else
			if findCurrentUI(uis.敌方英雄) or findCurrentUI(uis.敌方小兵) then
				local point = points.游戏中.方向键
				touchUp(1, point[1], point[2])
				local point = points.游戏中.一技能
				if point ~= nil then
					tap(point[1], point[2])
				end
				
				local point = points.游戏中.二技能
				if point ~= nil then
					tap(point[1], point[2])
				end
				
				local point = points.游戏中.三技能
				if point ~= nil then
					tap(point[1], point[2])
				end
				
				local point = points.游戏中.普通攻击
				if point ~= nil then
					tap(point[1], point[2])
				end
			else
				----没有发现敌方英雄 ,从小地图上获取敌方英雄头像
				--local enemyPoints = findColors({ 4, 2, 228, 228 },
				--        "0|0|0xf51a19,-2|-1|0xf81a19,-1|-2|0xee1d1f",
				--        95, 0, 0, 0)
				--if #enemyPoints ~= 0 then
				--    local miniPos = nil
				--    local mini_x, mini_y
				--    for var = 1, #enemyPoints do
				--        --sysLog(enemyPoints[var].x .. ":" .. enemyPoints[var].y)
				--        p2 = pos:new(enemyPoints[var].x, enemyPoints[var].y)
				--        local enemypos = p1:distanceBetween(p2)
				--        if miniPos == nil then
				--            miniPos = enemypos
				--            mini_x = enemyPoints[var].x
				--            mini_y = enemyPoints[var].y
				--        else
				--            if enemypos < miniPos then
				--                miniPos = enemypos
				--                mini_x = enemyPoints[var].x
				--                mini_y = enemyPoints[var].y
				--            end
				--        end
				--    end
				--
				--    print("MyPoint:" .. mePoint[1] .. " " .. mePoint[2] .. " " .. "EnemyPoint:" .. mini_x .. " " .. mini_y)
				--    local point = points.游戏中.方向键
				--    if point ~= nil then
				--        swip(point[1], point[2], point[1] + mini_x - mePoint[1], point[2] - mini_y - mePoint[2])
				--    end
				--else
				--
				--end
				
				mSleep(5000)
				local point = points.游戏中.方向键
				local 英雄被攻击 = findCurrentUI(uis.英雄被攻击)
				if 英雄被攻击 ~= nil then
					touchDown(1, point[1], point[2]);
					mSleep(200);
					touchMove(1, point[1] - 110, point[2] + 180)
					mSleep(1500);
					touchUp(1,point[1],point[2])
				else
					touchDown(1, point[1], point[2]);
					mSleep(200);
					touchMove(1, point[1] + 110, point[2] - 180)
					mSleep(500);
					--touchUp(1,point[1],point[2])
					
				end
				
				
			end
		end
		
		
	end
end
if ret == 1 then
	while true do
		mSleep(100)
		print("寻找任务中")
		--处理一些其他的点击事件
		findOtherPoint()
		if findCurrentUI(uis.游戏中) then
			sysLog("游戏中")
			fireinGame()
		else
			ControlChoose()
		end
	end
	
end